// import { rejects } from "assert";
// import { resolve } from "dns";
import LogUtil, { LOG_LEVEL } from "./LogUtil";
import { SingletonTemplet } from "./SingletonTemplet";

/**
 * 平台接口辅助类
 */
export default class PlatformHelper extends SingletonTemplet {
	constructor() {
		super();
	}
	public init():void{
		
	}
	/**
	 * 当前系统信息
	 * @returns 
	 */
	public getOsInfo(): UniNamespace.GetDeviceInfoResult {
		return uni.getDeviceInfo();
	}
	//-- 是否android平台
	public isAndroid(): boolean {
		return this.getOsInfo().platform === "app-android";
	};
	//-- 是否ios平台
	public isIos(): boolean {
		return this.getOsInfo().platform === "app-ios";
	}
	//-- 是否原生平台手机端
	public isNativeMobile(): boolean {
		return this.getOsInfo().platform === "app-ios" || this.getOsInfo().platform === "app-android";
	}
	//-- 是否是微信小程序端
	public isWechat(): boolean {
		return this.getOsInfo().platform === "mp-weixin";
	}
	//-- 请求设备震动
	/**
	 * 较短时间振动
	 * @param type 震动强度类型，有效值为：heavy、medium、light 必传
	 */
	public async shakeShort(type: string) {
		let promise:Promise<boolean> = new Promise((resolve,reject)=>{
			wx.vibrateShort({
				type: type,
				success: () => {
					resolve(true);
				},
				fail: () => { resolve(false);}
			})
		})
		return await promise;
	}
	/**
	 * 较长时间震动
	 */
	public shakeLong() {
		wx.vibrateLong();
	}

	//-- 设置屏幕亮度   0-100, 0-最暗，100-最大亮度
	public async setDeviceLight(lightNum: number):Promise<boolean> {
		let promise:Promise<boolean> = new Promise((resolve,rejects)=>{
			wx.setScreenBrightness({
				value: lightNum,
				success: () => { resolve(true) },
				fail: () => { resolve(false) }
			})
		})
		return await promise;
	}
	//-- 设置剪切板内容
	public async copyStr(str: string, showToast: boolean): Promise<boolean> {
		let isSuc: boolean = false;
		try {
			let promise: Promise<boolean> = new Promise((resolve, reject) => {
				uni.setClipboardData({
					data: str,
					showToast: showToast,
					success: () => { resolve(true); },
					fail: () => { resolve(false),LogUtil.log(LOG_LEVEL.ERROR,"copyStr",'设置剪切板内容失败'); }
				})
			})
			isSuc = await promise;
		} catch (e) {
			isSuc = false;
		}
		return isSuc;

	}
	//-- 获取剪切板内容
	public async getCopyStr(): Promise<string|null> {
		let str: string | null = null;
		try {
			let promise:Promise<string> = new Promise((resolve, rejects) => {
				uni.getClipboardData({
					success: (res) => { resolve(res.data); },
					fail: () => { rejects("出错了") },
				})
			});
			str = await promise;
		} catch (e) {
			LogUtil.log(LOG_LEVEL.ERROR, "getCompStr", e.message);
			str = null;
		}
		return str;
	}
	//-- 获取设备ip地址 格式  "ipv4|ipv6"  不存在  "null|null"
	public getDeviceIPAdressInfo(): string | null{
		uni.getNetworkType({
			
		})
		return '';
	}
	/**
	 * 获取当前网络状态  
	 * @returns 
	 */
	public async getNetworkType(): Promise<string | null> {
		let promise : Promise<string | null > = new Promise((resolve,reject)=>{
			uni.getNetworkType({
				success: (result) => {
					resolve(result.networkType)
				},
				fail: (result) => {
					resolve(null),
					LogUtil.log(LOG_LEVEL.ERROR,"getNetworkType","获取网络类型失败");
				}
			})
		})
		return await promise;
	}
	/**
	 * 保存一张图片到系统相册
	 * @param imgPath
	 * @return 保存后的路径 保存失败为空
	 */
	public async saveOnePictureToLocal(imgPath: string):Promise<string | null> {
		let promise:Promise<string | null> = new Promise((resolve,reject)=>{
			uni.saveImageToPhotosAlbum({
				filePath:imgPath,
				success: (result) => {
					resolve(result.path);
				},
				fail: (result) => {
					LogUtil.log(LOG_LEVEL.ERROR,"saveOnePictureToLocal","保存图片到本地失败"+ result);
					resolve(null);
				}
			})
		})
		return await promise;
		
	}
	
	/**
	 * 获取定位位置
	 * @param geocode 是否解析地址:默认为true
	 * @param isHighAccuracy 开启高精度定位:默认为true
	 * @param highAccuracyExpireTime 高精度定位 超时时间 超过3s生效
	 * @param altitude是否返回高度 :默认为false
	 * @param type 可选值 wgs84:返回gps坐标  gcj02 可用于uni.openLocation的坐标
	 */
	public async startGetLocation(geocode:boolean = true,isHighAccuracy:boolean = true, 
							highAccuracyExpireTime:number,altitude:boolean = false,
							type:string="wgs84"):Promise<UniApp.GetLocationSuccess | null> {
		let promise :Promise<UniApp.GetLocationSuccess | null> = new Promise((resolve,reject)=>{
			uni.getLocation({
				altitude:altitude ,//是否返回高度
				geocode:geocode,
				isHighAccuracy:isHighAccuracy,//开启高精度定位
				highAccuracyExpireTime:highAccuracyExpireTime,//高精度定位 超时时间 超过3s生效
				type:type,//返回gps坐标 gcj02 返回可用于uni.openLocation的坐标
				success: (result) => {
					resolve(result);
				},
				fail: (result) => {
					LogUtil.log(LOG_LEVEL.ERROR,"startGetLocation","获取定位失败");
					resolve(null);
				}
			})
		})
		return await promise;
		
	}
	/**
	 * 显示 loading
	 * @showMask 是否存在透明遮罩 防止 点击穿透
	 * @title 加载提示文字 默认 "拼命加载中"
	 */
	public async showLoadIng(showMask:boolean,title?:string):Promise<boolean>{
		let promise:Promise<boolean> = new Promise((resolve,rejects)=>{
			uni.showLoading({
				title:!!title? title : "拼命加载中",
				fail: () => {
					resolve(true);
				},
				mask:showMask,
				success: () => {
					resolve(false)
				},
			})
		})
		return await promise;
	}
}

export const platformHelper = PlatformHelper.getInstance();